import numpy as np
from lxml import etree
import os
import requests
import datetime
import random
import string
import time
from lxml import html
import logging
import diytool
from diytool import uuid_maker
from diytool import randomStr
from send_soap_xml import post_C2
a = 1
b = 1
def write_categroy_xml(path):

    parser = etree.XMLParser(encoding="utf-8")
    tree = etree.parse(r"/home/projects/xmlsource/category.xml", parser=parser)

    code = 'category' + uuid_maker(24)
    pcode = 'picture' + uuid_maker(25)

    programObjNode = tree.xpath('//Object[@ElementType="Category"]')[0]
    programObjNode.set("Code", code)
    programObjNode.set("ID", code)

    # 生成8位随机字符串
    categoryname = randomStr(8)
    categoryname = diytool.categoryNamelist[int(a)]
    name = tree.xpath('//Object[@ElementType="Category"]/Property[@Name="Name"]')[0]
    name.text = categoryname
    description = tree.xpath('//Object[@ElementType="Category"]/Property[@Name="Description"]')[0]
    description.text = categoryname

    picObjNode = tree.xpath('//Object[@ElementType="Picture"]')[0]
    picObjNode.set("Code", pcode)
    picObjNode.set("ID", pcode)

    mappingNode = tree.xpath('//Mapping[@ElementType="Category"][@ParentType="Picture"]')[0]
    mappingNode.set("ParentCode", pcode)
    mappingNode.set("ParentID", pcode)
    mappingNode.set("ElementCode", code)
    mappingNode.set("ElementID", code)

    ElementName = mappingNode.find('Property[@Name="ElementName"]')
    ElementName.text = categoryname

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')
    print("generate category: " + categoryname)
    list = [code,categoryname]
    return list

def write_dtl_xml(path,list,sequence):

    parser = etree.XMLParser(encoding="utf-8")
    tree = etree.parse(r"/home/projects/xmlsource/categorydtl.xml", parser=parser)

    code = 'program' + uuid_maker(26)
    mcode = 'movie' + uuid_maker(27)
    pcode = 'picture' + uuid_maker(25)

    programObjNode = tree.xpath('//Object[@ElementType="Program"]')[0]
    programObjNode.set("Code", code)
    programObjNode.set("ID", code)

    programname = diytool.programNamelist[int(b)]
    name = tree.xpath('//Object[@ElementType="Program"]/Property[@Name="Name"]')[0]
    name.text = programname
    description = programObjNode.find('Property[@Name="Description"]')
    description.text = programname + " is an excellent film, worthy of recommendation !"

    movieObjNode = tree.xpath('//Object[@ElementType="Movie"]')[0]
    movieObjNode.set("Code", mcode)
    movieObjNode.set("ID", mcode)
    mvname = movieObjNode.find('Property[@Name="Name"]')
    mvname.text = programname

    picObjNode = tree.xpath('//Object[@ElementType="Picture"]')[0]
    picObjNode.set("Code", pcode)
    picObjNode.set("ID", pcode)

    mappingNode1 = tree.xpath('//Mapping[@ElementType="Movie"][@ParentType="Program"]')[0]
    mappingNode1.set("ParentCode", code)
    mappingNode1.set("ParentID", code)
    mappingNode1.set("ElementCode", mcode)
    mappingNode1.set("ElementID", mcode)


    mappingNode2 = tree.xpath('//Mapping[@ElementType="Program"][@ParentType="Picture"]')[0]
    mappingNode2.set("ParentCode", pcode)
    mappingNode2.set("ParentID", pcode)
    mappingNode2.set("ElementCode", code)
    mappingNode2.set("ElementID", code)


    mappingNode3 = tree.xpath('//Mapping[@ElementType="Program"][@ParentType="Category"]')[0]
    mappingNode3.set("ParentCode", list[0])
    mappingNode3.set("ParentID", list[0])
    mappingNode3.set("ElementCode", code)
    mappingNode3.set("ElementID", code)
    ElementName = mappingNode3.find('Property[@Name="ElementName"]')
    ElementName.text = programname
    ParentName = mappingNode3.find('Property[@Name="ParentName"]')
    ParentName.text = list[1]
    sequenceNode = mappingNode3.find('Property[@Name="Sequence"]')
    if list[2] == '1':
        sequenceNode.text = str(sequence)
    else:
        sequenceNode.text = str(1000-int(sequence))
    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')
    print("generate program: " + name.text +" successfully")


while True:
    a = a + 1
    #request_url = "http://10.48.115.55:6080/axis/services/ctms"
    request_url = "http://172.19.64.71:8012/scms-injection-adapter/axis/services/ctms"
    CSPID = "10089"
    LSPID = "10089"
    CorrelateID = diytool.timeStr()
    # path = "D:/opt/wacos/worksheet/category" + uuid_maker(26) + ".xml"
    # list = write_categroy_xml(path)
    # path = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21")
    nowdate = datetime.datetime.now().strftime('%Y%m%d')
    hour = datetime.datetime.now().strftime('%H')
    #path ="D:/opt/wacos/worksheet/"
    path = "/opt/wacos/worksheet/category/" + nowdate+ "/" + hour + "/"
    xmlfile  = path + "category" + uuid_maker(25)+".xml"
    if not os.path.exists(path):
        print("# path not exists")
        os.makedirs(path)
    list=write_categroy_xml(xmlfile)
   # FileURL = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21") + xmlfilename
    #warnings.filterwarnings("ignore")
    FileURL ="ftp://wacos:wacos@172.19.97.208:21/" + xmlfile
    logging.info("category_worksheet_xml ftp file path-->>" + FileURL)
    post_C2(request_url, CSPID, LSPID, CorrelateID, FileURL)
    print("waiting for " + str(5) + "s ......")
    time.sleep(5)
    orderflag = '0'
    list.append(orderflag)
    total=1 #明细数量
    for i in range(1,total+1):
        nowdate = datetime.datetime.now().strftime('%Y%m%d')
        hour = datetime.datetime.now().strftime('%H')
        #path ="D:/opt/wacos/worksheet/"
        path = "/opt/wacos/worksheet/categorydtl/" + nowdate+ "/" + hour + "/"
        xmlfile  = path + "category" + uuid_maker(25)+".xml"
        if not os.path.exists(path):
           print("# path not exists")
           os.makedirs(path)
        xmlfile = path+"categorydtl"+uuid_maker(25) + ".xml"
        #path =  "D:/opt/wacos/worksheet/categorydtl" + uuid_maker(25) + ".xml"
        write_dtl_xml(xmlfile,list,i)
        fileurl = "ftp://wacos:wacos@172.19.97.208:21/" + xmlfile
        #path = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21")
        post_C2(request_url, CSPID, LSPID, CorrelateID, fileurl)
    b= b+1
    time.sleep(2)
    print("waiting for 10s... ")
    time.sleep(10)



